package com.dhruvtaara.beta.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.io.IOUtils;
import org.xml.sax.SAXException;

import com.dhruvtaara.beta.model.entities.Question;
import com.dhruvtaara.beta.model.persistance.Datastore;
import com.dhruvtaara.beta.toolset.QuestionParser;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;

public class FileUploadAction {

	/**
	 * 
	 * @param files
	 * @param fileType
	 * @throws FileUploadException
	 * @throws IOException
	 */
	public void doUpload(FileItemIterator files, String fileType)
			throws FileUploadException, IOException {
		try {
			FileItemStream file = files.next();
			InputStream fileStream = file.openStream();

			Blob fileBlob = new Blob(IOUtils.toByteArray(fileStream));
			fileStream.close();

			DatastoreService datastore = Datastore.get();

			Entity uploadedFile = new Entity("UploadedFile");
			uploadedFile.setProperty("fileName", file.getName());
			uploadedFile.setProperty("type", fileType);// e.g. question for
														// questions xml file
			uploadedFile.setProperty("contentType", file.getContentType());
			uploadedFile.setProperty("file", fileBlob);

			datastore.put(uploadedFile);

		} catch (FileUploadException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		}
	}

	/**
	 * 
	 * @param files
	 * @throws FileUploadException
	 * @throws IOException
	 * @throws ParserConfigurationException 
	 * @throws SAXException 
	 */
	public boolean doUpload(FileItemIterator files) throws FileUploadException,
			IOException, ParserConfigurationException, SAXException {
		try {
			if (files.hasNext()) {
				FileItemStream file = files.next();
				InputStream fileStream = file.openStream();
				//String fileContents = IOUtils.toString(fileStream);

				QuestionParser parser = new QuestionParser();
				List<Question> questions = parser.Parse(fileStream);
				
				fileStream.close();

				DatastoreService datastore = Datastore.get();
				List<Entity> questionEntities = new ArrayList<Entity>();

				for (Question question : questions) {
					Entity q = new Entity("Question");
					q.setProperty("text", question.getText());
					q.setProperty("answers", question.getAnswers());
					q.setProperty("choices", question.getChoices());
					q.setProperty("subject", question.getSubject());
					q.setProperty("hasMultipleAnswers",
							question.getHasMultipleAnswers());
					questionEntities.add(q);
				}

				datastore.put(questionEntities);

				return true;
			} else {
				return false;
			}

		} catch (FileUploadException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		}
	}
}
